package be.groept.ace.cursusplanning.model.containers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

import be.groept.ace.cursusplanning.model.Cursist;
import be.groept.ace.cursusplanning.utils.observer.EventArgs;
import be.groept.ace.cursusplanning.utils.observer.PropertyChanged;
import be.groept.ace.cursusplanning.utils.observer.Subject;
import be.groept.ace.cursusplanning.utils.observer.Subscriber;

public class Cursisten implements Container<Cursist, String> {

	private Map<String, Cursist> cursisten;
	
	private Subject<Cursist> aangeduideCursistSubject;
	
	/**
	 * Constructor
	 */
	public Cursisten() {
		cursisten = new HashMap<String, Cursist>();
		
		aangeduideCursistSubject = new PropertyChanged<Cursist>();
	}
	
	@Override
	public void add(Cursist cursist) throws IllegalArgumentException{
		if(cursist == null) throw new IllegalArgumentException("Cursist mag niet null zijn.");
		if(cursisten.containsKey(cursist.getCursistNummer())) throw new IllegalArgumentException("Cursist is al toegeveogd geweest.");
		cursisten.put(cursist.getCursistNummer(), cursist);
	}

	@Override
	public Cursist get(String key) throws IllegalArgumentException{
		if (key == null) throw new IllegalArgumentException("de key mag niet null zijn.");
		if(!cursisten.containsKey(key)) throw new IllegalArgumentException("Deze key bestaat niet");
		return cursisten.get(key);
	}

	@Override
	public void remove(String key) throws IllegalArgumentException{
		if (key == null) throw new IllegalArgumentException("de key mag niet null zijn.");
		if(!cursisten.containsKey(key)) throw new IllegalArgumentException("Deze key bestaat niet");
		cursisten.remove(key);
	}
	
	@Override
	public List<Cursist> getAll() {
		List<Cursist> cursistenLijst = new ArrayList<Cursist>();
		for(Iterator<String> i = cursisten.keySet().iterator(); i.hasNext();) {
			cursistenLijst.add(cursisten.get(i.next()));
		}
		return cursistenLijst;
	}

	@Override
	public Iterator<Cursist> iterator() {
		return cursisten.values().iterator();
	}
	
	public Cursist[] getAllArray() {
		return cursisten.values().toArray(new Cursist[cursisten.size()]);
	}
	
	public void addAangeduideCursistSubscriber(Subscriber<Cursist> subscriber) {
		aangeduideCursistSubject.addSubscriber(subscriber);
	}
	
	public void fireAangeduideCursistChanged(Cursist cursist) {
		aangeduideCursistSubject.notifySubscribers(new EventArgs<Cursist>(cursist));
	}

}
